#import "BYTextView.h"
#import "UITextView+Extension.h"
#import "CTKeyboardInputView.h"
@interface BYTextView () <UITextViewDelegate>
{
    NSUInteger currentCount;
    NSString * _defaultContent;
    NSInteger contentChangedNum;
}
@end
@implementation BYTextView
- (instancetype)init
{
    if (self = [super init]) {
        currentCount = 0;
        [self setupSubViews];
    }
    return self;
}
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UITextViewTextDidChangeNotification object:nil];
}
- (void)setupSubViews
{
    self.backgroundColor = [UIColor clearColor];
    [self addSubview:self.textView];
    [self addSubview:self.countLabel];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidHidden:) name:UIKeyboardWillHideNotification object:nil];
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textChangedExt:) name:UITextViewTextDidChangeNotification object:self.textView];
}
- (void)layoutSubviews {
    [super layoutSubviews];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(Ratio(46));
        make.right.mas_equalTo(Ratio(-46));
        make.top.mas_equalTo(self);
        make.bottom.mas_equalTo(Ratio(-30));
    }];
    [self.countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self);
    }];
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    CTKeyboardInputView *inputView = [[CTKeyboardInputView alloc] init];
    inputView.title = @"Other reason";
    inputView.contentPlaceHolder = @"Please enter a reason";
    inputView.maxInputLength =  100000;
    inputView.contentText = self.contentText;
    [inputView show];
    __weak typeof(self) weakSelf = self;
    inputView.inputTextEndBlock = ^(NSString * _Nonnull value) {
        weakSelf.contentText = value;
    };
    return;
}
#pragma mark - UIKeyboard
- (void)keyboardWillShow:(NSNotification *)notification{
}
- (void)keyboardDidHidden:(NSNotification *)notification{
}
- (void)textChangedExt:(NSNotification *)notification {
    NSInteger number = [self.textView.text length];
    NSString *str = [self textCountWithCurrentCount:number total:self.maxInputLength];
    self.countLabel.text = str;
}
- (NSString *)textCountWithCurrentCount:(NSUInteger)aCurrentCount total:(NSUInteger)aTotalCount
{
    if (aTotalCount == 0) {
        return @"";
    }
    if (aCurrentCount >= aTotalCount ) {
        NSString *tep = self.textView.text;
        NSString *newTemp = [tep substringToIndex:aTotalCount];
        self.textView.text = newTemp;
        self.countLabel.textColor = [UIColor redColor];
        aCurrentCount = aTotalCount;
    }
    else {
        self.countLabel.textColor = [UIColor colorWithHexString:@"#30323E"];
    }
    return [NSString stringWithFormat:@"%ld/%ld",aCurrentCount,aTotalCount];
}
#pragma mark - set&get
- (void)setContentPlaceHolder:(NSString *)contentPlaceHolder
{
    _contentPlaceHolder = contentPlaceHolder;
    UIColor * placeColor = [UIColor colorWithHexString:@"#30323E"];
    [self.textView was_setPlaceholderWithText:contentPlaceHolder Color:placeColor];
}
- (void)setMaxInputLength:(NSInteger)maxInputLength
{
    _maxInputLength = maxInputLength;
    self.countLabel.text = [self textCountWithCurrentCount:currentCount total:maxInputLength];
}
- (void)setContentText:(NSString *)contentText
{
    _contentText = contentText;
    self.textView.placeholderLabel.hidden = !(contentText.length == 0);
    if (contentChangedNum == 0) {
        _defaultContent = contentText;
    }
    if (contentText.length) {
        _textView.text = contentText;
        currentCount = [self.textView.text length];
        _countLabel.text = [self textCountWithCurrentCount:currentCount total:self.maxInputLength];
        if (self.inputTextComplete) {
            self.inputTextComplete(contentText);
        }
    }
}
- (UILabel *)countLabel
{
    if (!_countLabel) {
        _countLabel = [UILabel new];
        _countLabel.font = [UIFont systemFontOfSize:16.f];
        _countLabel.textColor = [UIColor colorWithHexString:@"#30323E"];
    }
    return _countLabel;
}
- (UITextView *)textView
{
    if (!_textView) {
        _textView = [[UITextView alloc] init];
        _textView.backgroundColor = [UIColor colorWithHexString:@"#F4F6FB"];
        _textView.delegate = self;
        _textView.font = [UIFont systemFontOfSize:16.f];
        _textView.textColor = [UIColor colorWithHexString:@"#8E93A1"];
        _textView.delegate = self;
        _textView.userInteractionEnabled = NO;
    }
    return _textView;
}
- (NSString *)text {
    if (!_text) {
        _text = self.textView.text;
    }
    return _text;
}
@end
